/**
 *
 * Copyright (c) 2012 NCS Corporation.
 *
 * Author: tangb
 * Created Date: May 30, 2012
 * 
 * 	version					author					remark
 * 	1.0						tangb				
 */
package com.ocgs.base.util;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;

public class FileUtil extends FileUtils {
	/**
	 * Automatically generated variable: ONE_K_BYTES
	 */
	private static final int ONE_K_BYTES = 1024;
	private static final Logger log = Logger.getLogger(FileUtil.class);
	public static  int OUTPUT_BYTE_ARRAY_INITIAL_SIZE=4096;
	
	public static void getFielContentAsBytes(String file, OutputStream os) throws Exception {
		File srcFile = new File(file);
		InputStream inPut = new FileInputStream(srcFile);
		try {
			byte[] bytes = new byte[ONE_K_BYTES];
			int i;
			while ((i = inPut.read(bytes)) != -1) {
				os.write(bytes, 0, i);
			}
		} catch (IOException ie) {
			log.error("error in getFielContentAsBytes", ie);
		} finally {
			inPut.close();
		}
	}

	/**
	 * This method is model copy file
	 *
	 *            srcPath, must including the source file path and name.
	 * @throws Exception
	 * @throws Exception
	 */
	public static void copyFile(String srcPath, String destPath) throws Exception {
		FileInputStream inPut = null;
		FileOutputStream outPut = null;
		try {

			inPut = new FileInputStream(srcPath);
			outPut = new FileOutputStream(destPath);
			byte[] bytes = new byte[ONE_K_BYTES];
			int i;
			while ((i = inPut.read(bytes)) != -1) {
				outPut.write(bytes, 0, i);
			}
		} catch (Exception e) {
			log.error("", e);
			throw e;
		} finally {
			try {
				if (inPut != null)
					inPut.close();
				if (outPut != null)
					outPut.close();
			} catch (IOException e2) {
				// TODO: handle exception
				if (outPut != null)
					outPut.close();
				log.error("", e2);
				throw e2;
			}

		}
	}

	/**
	 * This method is model delete file
     * *	 * @param String
	 *            srcPath, must including the source file path and name.
	 * @throws Exception
	 * @return boolean
	 */
	public static boolean deleteFile(String srcPath) throws Exception {
		boolean isDelete = false;
		try {
			File inPut = new File(srcPath);
			isDelete = inPut.delete();
		} catch (Exception e) {
			log.error("", e);
			throw e;
		}
		return isDelete;
	}

	public static String getFullFileName(String filePath, String fileName) {
		filePath = filePath.replaceAll("\\\\", "/");
		if (!filePath.endsWith("/"))
			filePath = filePath + "/";
		return filePath + fileName;
	}

	/**
	 * This method is model move file from one folder model the other
	 *
	 *            srcPath, must including the source file path and name.
	 *            destPath, must including the destination file path and name.
	 * @throws Exception
	 */
	public static void moveFile(String srcPath, String destPath) throws Exception {
		copyFile(srcPath, destPath);
		deleteFile(srcPath);
	}

	/**
	 * This method is model get the file size, represent as bytes.
	 *
	 *            srcPath
	 * @return long length of the file
	 * @throws Exception
	 */
	public static long getFileSize(String srcPath) throws Exception {
		try {
			File inputFile = new File(srcPath);
			return inputFile.length();
		} catch (Exception e) {
			log.error("", e);
			throw e;
		}
	}

	/**
	 * This method is model get the last modified date of the file
	 * 
	 *            srcPath
	 * @return Date the last modified date of the file
	 * @throws Exception
	 */
	public static Date getFileLastModifiedTime(String srcPath) throws Exception {
		try {
			File inputFile = new File(srcPath);
			return new Date(inputFile.lastModified());
		} catch (Exception e) {
			log.error("", e);
			throw e;
		}
	}

	public static boolean existFile(String fullFileName) {
		File f = new File(fullFileName);
		return f.exists();
	}

	public static byte[] convertToByte(File file) {
		if (file == null)
			return null;
		BufferedInputStream in = null;
		ByteArrayOutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(file));
			out = new ByteArrayOutputStream(1024);
			byte[] temp = new byte[1024];
			int size;
			while ((size = in.read(temp)) != -1) {
				out.write(temp, 0, size);
			}
			return out.toByteArray();
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} finally {
			try {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}
	
	
	/**
	 * 
	 * @title viewFile
	 * @Description view file,mengxuan add
	 * @throws Exception
	 */
	public static void viewFile(String filePath,String fileName) throws Exception{
		String fullFilepath=filePath+File.separator+fileName;
		ByteArrayOutputStream out = new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
		InputStream io=new FileInputStream(new File(fullFilepath));
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setHeader("Content-disposition","attachment;filename="+ new String(fileName.getBytes("UTF-8"), "ISO_8859_1"));
		response.setContentType("application/octet-stream");
		inputStreamToArrayOutputStream(io,out);
		response.setContentLength(out.size());
		
		OutputStream os=response.getOutputStream();
		out.writeTo(os);
		out.close();
		os.close();
	}
	
	public static void inputStreamToArrayOutputStream(InputStream io,ByteArrayOutputStream out) throws IOException{
		byte[] buff = new byte[8000];            
		int bytesRead = 0;            
		while((bytesRead = io.read(buff)) != -1) {              
			out.write(buff, 0, bytesRead);           
		}            
	}
}
